package com.cn.http.service.impl;

import cn.com.allunion.sms.pojo.Smsrecord;
import com.alibaba.fastjson.JSON;
import com.cn.http.service.AbstractSmsSendService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 电信官方短信发送服务
 * Created by YangJie on 2016/4/21.
 */
public class TelecomSmsSendService extends AbstractSmsSendService {
    private static final Logger LOGGER = LoggerFactory.getLogger(TelecomSmsSendService.class) ;

    private final String smsDefaultSign ;

    public TelecomSmsSendService(String smsDefaultSign) {
        this.smsDefaultSign = smsDefaultSign;
    }

    @Override
    public String send(List<Smsrecord> smsrecords) {
//        LOGGER.debug("dianxin:item" + item);
//        LOGGER.debug("dianxin:sign" + sign);
        String result = null;

        if (smsrecords == null || smsrecords.size() == 0) {
            return "sms记录为空";
        }
        /**
         * 单发短信功能
         */
        for (Smsrecord smsRecord2 : smsrecords) {
            String res = smsrecordService.addNewRecord(smsRecord2);
//            String sendNumber = smsRecord2.getSendNumber();
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("短信记录ID： {}", smsRecord2.getSmsRecordID());
                LOGGER.debug("短信单发电话号码处理前: {}", smsRecord2.getSendNumber());
                LOGGER.debug("电信号码不加86");
            }

            if (res.equals("success")) {
//                String mess = "{'operating':'null','smsOperator':" + "'"
//                        + smsRecord2.getSmsOperator().getJoinNum() + "'"
//                        + ",'operatorMessageID':" + "'"
//                        + smsRecord2.getOperatorMessageID() + "'"
//                        + ",'sendContent':" + "'"
//                        + smsRecord2.getSendContent() + "'"
//                        + ",'sendNumber':" + "'" + sendNumber + "'"
//                        + ",'sendState':" + "'" + smsRecord2.getSendState()
//                        + "'" + ",'smsRecordID':" + "'"
//                        + smsRecord2.getSmsRecordID() + "'"
//                        + ",'businessRecord':" + "'"
//                        + smsRecord2.getSmsBusinessRecord().getBusinessCode()
//                        + "'" + ",'sign':" + "'【" + smsDefaultSign + "】'" + "}";

                // 发送消息到消息队列
                String message = getSendMessage(smsRecord2, smsDefaultSign) ;
                if(LOGGER.isDebugEnabled()) {
                    LOGGER.debug("message： {}", message);
                }

                // 发送消息到消息队列
                if (StringUtils.isNotEmpty(message)) {
                    sendSMS.send(message);
                    result = "接收成功！";
                } else {
                    result = "接收失败，无法获取转换后的消息， smsrecord: " + JSON.toJSONString(smsRecord2);
                }
            } else {
                result = "发送之前存储失败";
                LOGGER.debug("发送之前存储失败");
            }
        }
        return result;
    }

    public String getSmsDefaultSign() {
        return smsDefaultSign;
    }
}
